const set = new Set([1, 2, 3, 4, 4, 5])
console.log([...set]) // [1, 2, 3, 4, 5]
console.log(set.size) // 5

const sets = new Set()
sets.add(NaN)
sets.add(NaN)
sets.add(12)
console.log(sets) // sets Set { NaN, 12 }

// 另外，两个对象总是不相等的
sets.add({})
sets.add({})
console.log(sets) // Set { NaN, 12, {}, {} }

const set2 = new Set()
set2.add(1).add(2).add(2)
console.log(set2.size) // 2
console.log(set2.has(1)) // true
console.log(set2.has(2)) // true
console.log(set2.has(3)) // false

set2.delete(2)
console.log(set2.has(2)) // false

const items = new Set([1, 2, 3, 3, 4])
console.log(Array.from(items)) // [1, 2, 3, 4]


{
  let set = new Set(['red', 'green', 'blue'])
  console.log(set.keys()) // [Set Iterator] { 'red', 'green', 'blue' }
  for (let item of set.keys()) {
    console.log('keys()', item)
  }
  // red
  // green
  // blue
  for (let item of set.values()) {
    console.log('values()', item)
  }
  // red
  // blue
  //green
  for (let item of set.entries()) {
    console.log('entries()', item)
  }
  // [red, red]
  // [green, green]
  // [blue, blue]
}

{
  // 使用set可以很容易实现并集，交集
  let a = new Set([1, 2, 3])
  let b = new Set([4, 3, 2])
  // 并集
  let union = new Set([...a, ...b])
  console.log('union', union) // { 1, 2, 3, 4 }
  // 交集
  let intersect = new Set([...a].filter(x => b.has(x)))
  console.log('intersect', intersect) // { 2, 3 }
}







